package org.uni_hi.lsep.gui.test.sim;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.uni_hi.lsep.gui.util.VerifiedTextField;
import org.uni_hi.lsep.util.IPAddress;
import org.uni_hi.lsep.util.IPAddressSyntaxException;

public class IPInputVerifier extends InputVerifier {

	@Override
	public boolean verify(JComponent jComp) {
		JTextField tf=(JTextField) jComp;
		String ip=tf.getText();
		if(ip.equals(""))return true;
		IPAddress adr=null;
		try {
			adr=IPAddress.parse(ip,jComp.getName());
			((VerifiedTextField) tf.getParent()).setSelectedItem(adr);
		} catch (IPAddressSyntaxException e) {
			JOptionPane.showMessageDialog(jComp.getRootPane(), e.getMessage(), "Warnung", JOptionPane.WARNING_MESSAGE);
			tf.selectAll();
		}
		return adr!=null;
	}
}
